@cls
@echo off
setlocal 
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Description: Install arduino libraries in Atmel Studio 6.0 
REM Version: 1.0
REM Author: Omar Francisco
REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

echo **************************************************************
echo Arduino Installer For Atmel Studio 6 - Version 1.00
echo Created by Omar Francisco - 08/11/2012
echo **************************************************************

call :SetUserVariables
call :SetScriptVariables
call :CheckFilesAndDirectories||exit /b 1
call :UnzipCoreLibraryPackage
call :UnzipArduinoLibraries
call :CopyPinsArduino
call :GenerateReplacementScritp
call :GenerateSketchTemplate
call :GenerateLibraryTemplate
call :GenerateDeployScript
call :GenerateDeployAVRScript
call :DecompressLibrarySourceCode
call :ModifyLibraryProjects
call :ShowInstallResults
goto :END

:SetUserVariables
REM Variables that may be changed by user
echo - Setting User Variables
REM typical values are ATmega32u4 (Leonardo),  ATMega328p (Uno), ATmega2560 (Mega), ATmega168 (Diecimila)
set ATMEL_CPU=ATmega328P
set CPU_FREQUENCY=16000000
set ARDUIN0_VERSION=100
set COM_PORT=COM3
set COM_PORT_SPEED=115200
REM Possible valies standard|mega|leonardo\eightanaloginputs
set VARIANT=standard
set AVRDUDE_ROOT_DIR=C:\arduino-1.0.1\hardware\tools\avr
goto :END

:SetScriptVariables
echo - Setting Script Variables
REM Set Script Variables
set ATMEL_STUDIO=Atmel Studio
set ATMEL_STUDIO_PATH=%USERPROFILE%\Documents\%ATMEL_STUDIO%
set TEMPLATE_PATH=%ATMEL_STUDIO_PATH%\Templates\ProjectTemplates

REM Change these variables as needed
set ARDUINO_LIBRARY_SOURCE_PATH=%ATMEL_STUDIO_PATH%\ArduinoLibrariesSource
set LIB_PATH=%ATMEL_STUDIO_PATH%\Libs
set HEADER_PATH=%ATMEL_STUDIO_PATH%\Headers

REM Do not change
set ARDUINO_CORE_PATH=%LIB_PATH%\ArduinoCore
set ARDUINO_LIBS_PATH=%LIB_PATH%\Arduino
set PUBLIC_LIBS_PATH=%LIB_PATH%\Public
set ARDUINO_CORE_HEADER_PATH=%HEADER_PATH%\ArduinoCore
set ARDUINO_LIBS_HEADER_PATH=%HEADER_PATH%\Arduino
set PUBLIC_LIBS_HEADER_PATH=%HEADER_PATH%\Public
set PINS_FILE=%VARIANT%_pins_arduino.h
set SED_SCRIPT=replace.sed
set DEPLOY_TEMPLATE=deploytemplate.txt
set DEPLOY_SCRIPT=%ATMEL_STUDIO_PATH%\deploy.bat
set LOCAL_DEPLOY_TEMPLATE=deploy.template.txt
set LOCAL_DEPLOY_SCRIPT=localdeploy.bat
REM Commands
set DECOMPRESS=7z.exe x
set COMPRESS=7z.exe a -tzip
set FILE_EDITOR=sed
REM Important Files
set LIBCORE_PACKAGE=libcore.zip
set LIBCORE_HEADER_PACKAGE=ArduinoCoreHeaders.zip
set LIBS_PACKAGE=ArduinoLibs.zip
set LIBS_HEADER_PACKAGE=ArduinoLibsHeaders.zip
set ARDUINO_LIBRARY_SOURCE_PACKAGE=ArduinoLibrariesSource.zip
REM Substution files
SET SKETCH_TEMPLATE=ArduinoSketchTemplate.zip
SET SKETCH_TEMPLATE_NAME="Arduino Sketch.zip"
set SKETCH_PROJECT_FILE=ArduinoTemplate.cppproj
set SKETCH_PROJECT_TEMPLATE=ArduinoSketchTemplate.txt
SET LIB_TEMPLATE=ArduinoLibraryTemplate.zip
set LIB_TEMPLATE_NAME="Arduino Library.zip"
set LIB_PROJECT_FILE=ArduinoLibTemplate.cppproj
set LIB_PROJECT_TEMPLATE=ArduinoLibTemplate.txt
set WORKING_DIR=temp
goto :END

:CheckFilesAndDirectories

echo - Checking Files And Directories
REM Check directories exist
if NOT exist "%ATMEL_STUDIO_PATH%" (
echo ************************************[ERROR]*************************************
echo "%ATMEL_STUDIO_PATH%" not found.  - aborting installation script
echo ********************************************************************************
exit /B 1
)

if NOT exist "%TEMPLATE_PATH%" (
echo ************************************[ERROR]*************************************
echo "%TEMPLATE_PATH%" not found.  - aborting installation script
echo ********************************************************************************
exit /B 1
)

REM Check variant is valid
if NOT exist %PINS_FILE% (
echo ************************************[ERROR]*************************************
echo The pin definition for the variant specified %PINS_FILE%   
echo does not exist - aborting installation script
echo ********************************************************************************
exit /B 1
)
REM Check deploy template exist
if NOT exist %DEPLOY_TEMPLATE% (
echo ************************************[ERROR]*************************************
echo The deploy template %DEPLOY_TEMPLATE% could not be found  
echo aborting installation script
echo ********************************************************************************
exit /B 1
)

REM Delete core and library paths if they exist.  Leave public library folder alone
REM as it may have personal libraries that are not Arduino related.  This will address
REM reinstallation of the arduino libraries without affecting user developed libraries.
if exist "%ARDUINO_CORE_PATH%" rmdir /s /Q "%ARDUINO_CORE_PATH%" > NUL
if exist "%ARDUINO_LIBS_PATH%" rmdir /s /Q "%ARDUINO_LIBS_PATH%" > NUL
if exist "%ARDUINO_CORE_HEADER_PATH%" rmdir /s /Q "%ARDUINO_CORE_HEADER_PATH%" > NUL
if exist "%ARDUINO_LIBS_HEADER_PATH%" rmdir /s /Q "%ARDUINO_LIBS_HEADER_PATH%" > NUL
if exist "%ARDUINO_LIBRARY_SOURCE_PATH%" rmdir /s /Q "%ARDUINO_LIBRARY_SOURCE_PATH%" > NUL


REM Create directory structure
if NOT exist "%LIB_PATH%" md "%LIB_PATH%" > NUL
if NOT exist "%ARDUINO_CORE_PATH%" md "%ARDUINO_CORE_PATH%" > NUL
if NOT exist "%ARDUINO_LIBS_PATH%" md "%ARDUINO_LIBS_PATH%" > NUL
if NOT exist "%PUBLIC_LIBS_PATH%" md "%PUBLIC_LIBS_PATH%" > NUL
if NOT exist "%HEADER_PATH%" md "%HEADER_PATH%" > NUL
if NOT exist "%ARDUINO_CORE_HEADER_PATH%" md "%ARDUINO_CORE_HEADER_PATH%" > NUL
if NOT exist "%ARDUINO_LIBS_HEADER_PATH%" md "%ARDUINO_LIBS_HEADER_PATH%" > NUL
if NOT exist "%PUBLIC_LIBS_HEADER_PATH%" md "%PUBLIC_LIBS_HEADER_PATH%" > NUL
if NOT exist "%ARDUINO_LIBRARY_SOURCE_PATH%" md "%ARDUINO_LIBRARY_SOURCE_PATH%" > NUL

goto :END

:UnzipCoreLibraryPackage
echo - Unziping Core Library Package
REM unzip content of ArduinoCore
%DECOMPRESS% "%LIBCORE_PACKAGE%" -o"%ARDUINO_CORE_PATH%" -y > NUL
%DECOMPRESS% "%LIBCORE_HEADER_PACKAGE%"  -o"%ARDUINO_CORE_HEADER_PATH%" -y > NUL
goto :END

:UnzipArduinoLibraries
echo - Unziping Arduino libraries
%DECOMPRESS% "%LIBS_PACKAGE%" -o"%ARDUINO_LIBS_PATH%" -y > NUL
%DECOMPRESS% "%LIBS_HEADER_PACKAGE%" -o"%ARDUINO_LIBS_HEADER_PATH%" -y > NUL
goto :END

:CopyPinsArduino
echo - Copy pins_arduino.h File
copy %PINS_FILE% "%ARDUINO_CORE_HEADER_PATH%\pins_arduino.h" /Y > NUL
goto :END

:GenerateReplacementScritp
echo - Generate SED replacement script
REM Generate SED replace script
SET DELIM=/
echo s%DELIM%$(ATMEL_CPU)%DELIM%%ATMEL_CPU%%DELIM%g > %SED_SCRIPT%
echo s%DELIM%$(FREQUENCY)%DELIM%%CPU_FREQUENCY%%DELIM%g >> %SED_SCRIPT%
echo s%DELIM%$(ARDUINO_VERSION)%DELIM%%ARDUIN0_VERSION%%DELIM%g >> %SED_SCRIPT%

echo s%DELIM%$(COM_PORT)%DELIM%%COM_PORT%%DELIM%g >> %SED_SCRIPT%
echo s%DELIM%$(COM_PORT_SPEED)%DELIM%%COM_PORT_SPEED%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %ARDUINO_CORE_HEADER_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_CORE_HEADER_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %ARDUINO_LIBS_HEADER_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_LIB_HEADER_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %ARDUINO_CORE_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_CORE_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %ARDUINO_LIBS_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_LIB_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %PUBLIC_LIBS_HEADER_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_PUBLIC_LIB_HEADER_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %PUBLIC_LIBS_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(ARDUINO_PUBLIC_LIB_PATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %AVRDUDE_ROOT_DIR%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(AVRDUDEROOT)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %LIB_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(LIBPATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%

for /f "delims=" %%a  in ('echo %HEADER_PATH%^| sed "s!\\!\\\\!g"') do @set TARGETPATH=%%a
echo s%DELIM%$(HEADERPATH)%DELIM%%TARGETPATH%%DELIM%g >> %SED_SCRIPT%
goto :END

:GenerateSketchTemplate
echo - Generate Sketch Template
REM Decompress ARDUINO SKETCH TEMPLATE
if exist "%WORKING_DIR%" rmdir /S /Q "%WORKING_DIR%" > NUL
if NOT exist "%WORKING_DIR%" md "%WORKING_DIR%" > NUL
%DECOMPRESS% "%SKETCH_TEMPLATE%" -o"%WORKING_DIR%\" -y  > NUL
:::Template
%FILE_EDITOR% -f %SED_SCRIPT% < %SKETCH_PROJECT_TEMPLATE% > %WORKING_DIR%\%SKETCH_PROJECT_FILE%
:::deploy.bat
%FILE_EDITOR% -f %SED_SCRIPT% < %WORKING_DIR%\%LOCAL_DEPLOY_TEMPLATE% > %WORKING_DIR%\%LOCAL_DEPLOY_SCRIPT%
del %WORKING_DIR%\%LOCAL_DEPLOY_TEMPLATE% /F > NUL
REM Compress sketch template and move to Project Templates directory
cd %WORKING_DIR%
..\%COMPRESS% %SKETCH_TEMPLATE_NAME% *.* > NUL
move /Y %SKETCH_TEMPLATE_NAME% "%TEMPLATE_PATH%" > NUL
cd ..
if exist "%WORKING_DIR%" rmdir /S /Q "%WORKING_DIR%" > NUL
goto :END

:GenerateLibraryTemplate
echo - Generate Library Template
REM Decompress ARDUINO LIBRARY TEMPLATE
if exist "%WORKING_DIR%" rmdir /S /Q "%WORKING_DIR%" > NUL
if NOT exist "%WORKING_DIR%" md "%WORKING_DIR%" > NUL
%DECOMPRESS% "%LIB_TEMPLATE%" -o"%WORKING_DIR%\" -y  > NUL
:::Template
%FILE_EDITOR% -f %SED_SCRIPT% < %LIB_PROJECT_TEMPLATE% > %WORKING_DIR%\%LIB_PROJECT_FILE%
:::deploy.bat
%FILE_EDITOR% -f %SED_SCRIPT% < %WORKING_DIR%\%LOCAL_DEPLOY_TEMPLATE% > %WORKING_DIR%\%LOCAL_DEPLOY_SCRIPT%
del %WORKING_DIR%\%LOCAL_DEPLOY_TEMPLATE% /F > NUL
REM Compress sketch template and move to Project Templates directory
cd %WORKING_DIR%
..\%COMPRESS% %LIB_TEMPLATE_NAME% *.* > NUL
move /Y %LIB_TEMPLATE_NAME% "%TEMPLATE_PATH%" > NUL
cd ..
if exist "%WORKING_DIR%" rmdir /S /Q "%WORKING_DIR%" > NUL
goto :END


:GenerateDeployScript
echo - Generate Deploy Script
REM configure global deploy script
%FILE_EDITOR% -f %SED_SCRIPT% < %DEPLOY_TEMPLATE% > "%DEPLOY_SCRIPT%"
goto :END

:GenerateDeployAVRScript
echo - Generate Deploy Script For AVR projects
REM configure global deploy script
REM copy deploy-avr.bat "%ATMEL_STUDIO_PATH%\" /Y > NUL
goto :END

:DecompressLibrarySourceCode
echo - Decompressing Arduino Library Source Code
%DECOMPRESS% "%ARDUINO_LIBRARY_SOURCE_PACKAGE%" -o"%ARDUINO_LIBRARY_SOURCE_PATH%\"  -y > NUL
goto :END

:ModifyLibraryProjects
setlocal ENABLEDELAYEDEXPANSION
echo - Modifying library projects
:::echo on
:::for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
for /f "delims=|" %%f in ('dir /s /b "%ARDUINO_LIBRARY_SOURCE_PATH%"\*.template') do (
set VAR1=%%f
set PROJECT_FULL_PATHS=!VAR1:.template=!
%FILE_EDITOR% -f %SED_SCRIPT% < !VAR1! > "!PROJECT_FULL_PATHS!"
del "%%f" /Q
echo off
)
:::echo off
goto :END

:ShowInstallResults
echo - Installation Complete
echo --------
echo [Arduino Core Library]
echo %ARDUINO_CORE_PATH%
echo --------
echo [Arduino Libraries]
echo %ARDUINO_LIBS_PATH%
echo --------
echo [Public Libraries]
echo %PUBLIC_LIBS_PATH%
echo --------
echo [Arduino Core Library Headers]
echo %ARDUINO_CORE_HEADER_PATH%
echo --------
echo [Arduino Library Headers]
echo %ARDUINO_LIBS_HEADER_PATH%
echo --------
echo [Public Headers]
echo %PUBLIC_LIBS_HEADER_PATH%
echo --------
echo [Global Deploy Script]
echo %DEPLOY_SCRIPT%
echo --------
goto :END


:END



